/*
 * @Author: Gjx 1308613747@qq.com
 * @Date: 2022-07-07 17:07:04
 * @LastEditors: Gjx 1308613747@qq.com
 * @LastEditTime: 2022-07-07 18:11:20
 * @FilePath: /jsCode/eas/encrypted.js
 * @Description: 这是默认设置,请设置`customMade`, 打开koroFileHeader查看配置 进行设置: https://github.com/OBKoro1/koro1FileHeader/wiki/%E9%85%8D%E7%BD%AE
 */
const AES_KEY = '27jrWz3sxrVbR+pn';
let AES_IV = ''; // 12位 IV 偏移量

// // 生成指定位数的随机字符串
function renderNumber(n = 0) {
    let rn = '';
    if (!n) {
        return rn
    }
    let chars = ['0','1','2','3','4','5','6','7','8','9','A','B','C','D','E','F','G','H','I','J','K','L','M','N','O','P','Q','R','S','T','U','V','W','X','Y','Z','a','b','c','d','e','f','g','h','i','j','k','m','n','p','r','s','t','u','v','w','x','y','z'];
    for (let i=0; i<n; i++){
        let id = Math.ceil(Math.random()*58);
        rn +=  chars[id]
    }
    return rn;
}

let AES = {
    // CBC 加密
    CBCEncrypt: function (key, iv, data) {
        // 统一将传入的字符串转成UTF8编码
        const dataHex = CryptoJS.enc.Utf8.parse(data); // 需要加密的数据
        const keyHex = CryptoJS.enc.Utf8.parse(key); // 秘钥
        const ivHex = CryptoJS.enc.Utf8.parse(iv); // 偏移量
        const encrypted = CryptoJS.AES.encrypt(dataHex, keyHex, {
            iv: ivHex,
            mode: CryptoJS.mode.CBC, // 加密模式
            padding: CryptoJS.pad.Pkcs7,
        });
        let encryptedVal = encrypted.ciphertext.toString();
        return encryptedVal; //  返回加密后的值
    },
    // CBC 解密
    CBCDecrypt: function (key, iv, encryptedVal) {
        // 传入的key和iv需要和加密时候传入的key一致
        // 统一将传入的字符串转成UTF8编码
        let encryptedHexStr = CryptoJS.enc.Hex.parse(encryptedVal);
        let srcs = CryptoJS.enc.Base64.stringify(encryptedHexStr);
        const keyHex = CryptoJS.enc.Utf8.parse(key); // 秘钥
        const ivHex = CryptoJS.enc.Utf8.parse(iv); // 偏移量
        let decrypt = CryptoJS.AES.decrypt(srcs, keyHex, {
            iv: ivHex,
            mode: CryptoJS.mode.CBC,
            padding: CryptoJS.pad.Pkcs7,
        });
        let decryptedStr = decrypt.toString(CryptoJS.enc.Utf8);
        return decryptedStr.toString();
    },
    // ECB 加密
    ECBEncrypt: function (key,text){
        let keyHex = CryptoJS.enc.Base64.parse(key);
        let messageHex = CryptoJS.enc.Utf8.parse(text);
        let encrypted = CryptoJS.AES.encrypt(messageHex, keyHex, {
            mode: CryptoJS.mode.ECB,
            padding: CryptoJS.pad.Pkcs7,
        });
        return encrypted.toString();
    },
    // ECB 解密
    ECBDecrypt: function (key,encryptedVal){
        let keyHex = CryptoJS.enc.Base64.parse(key);
        let decrypt = CryptoJS.AES.decrypt(encryptedVal, keyHex, {
            mode: CryptoJS.mode.ECB,
            padding: CryptoJS.pad.Pkcs7,
        });
        return CryptoJS.enc.Utf8.stringify(decrypt)
    }
};

function encryptedAES(msg) {
    if (typeof msg === 'object') {
        // 如果传入的data是json对象，先转义为json字符串
        try {
            msg = JSON.stringify(msg);
        } catch (error) {
            console.log('error:', error);
        }
    }

    const key = 'f4k9f5w7f8g4er26'; //十六位十六进制数作为密钥
    const iv = '1234567890qwerty'; //十六位十六进制数作为密钥偏移量
    // CBC
    let cbcEncrypt = AES.CBCEncrypt(key, iv, msg);
    console.log("CBC 加密结果", cbcEncrypt);
    let cbcText = AES.CBCDecrypt(key, iv, cbcEncrypt);
    console.log("CBC 解密结果", cbcText);

    // ECB
    let ecbEncrypt = AES.ECBEncrypt(key, msg);
    console.log("ECB 加密结果", ecbEncrypt);
    let ecbText = AES.ECBDecrypt(key, ecbEncrypt);
    console.log("ECB 解密结果", ecbText);

    // return enText
}

// encryptedAES('123456');
